package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.alipay.api.internal.util.AlipayUtils;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.*;
import com.api.shopapi.service.*;
import com.api.shopapi.util.AliPayUtils;
import com.api.shopapi.util.WechatPayUtils;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 银秋长者
 * */


@RestController
@RequestMapping("AppCare")
public class AppCareController extends BaseController {


    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;


    @Autowired
    @Qualifier("UserAddressServiceImplOne")
    private UserAddressService userAddressService;


    @Autowired
    @Qualifier("UserAppInfoServiceImplOne")
    private UserAppInfoService userAppInfoService;


    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;





    @Autowired
    @Qualifier("CartServiceImplOne")
    private CartService cartService;

    @Autowired
    @Qualifier("CartBusinessServiceImplOne")
    private CartBusinessService cartBusinessService;

    @Autowired
    @Qualifier("CartGoodsServiceImplOne")
    private CartGoodsService cartGoodsService;


    @Autowired
    @Qualifier("OrderServiceImplOne")
    private OrderService orderService;

    @Autowired
    @Qualifier("OrderBusinessServiceImplOne")
    private OrderBusinessService orderBusinessService;

    @Autowired
    @Qualifier("OrderGoodsServiceImplOne")
    private OrderGoodsService orderGoodsService;

    @Autowired
    @Qualifier("OrderTradenoServiceImplOne")
    private OrderTradenoService orderTradenoService;

    @Autowired
    @Qualifier("OrderBusinessTradenoRelServiceImplOne")
    private OrderBusinessTradenoRelService orderBusinessTradenoRelService;

    @Autowired
    @Qualifier("OrderServeServiceImplOne")
    private OrderServeService orderServeService;

    @Autowired
    @Qualifier("OrderBusinessAppraiseServiceImplOne")
    private OrderBusinessAppraiseService orderBusinessAppraiseService;

    @Autowired
    @Qualifier("GoodsPriceServiceImplOne")
    private GoodsPriceService goodsPriceService;






    @Autowired
    @Qualifier("GoodsServiceImplOne")
    private GoodsService goodsService;

    @Autowired
    @Qualifier("GoodsClassServiceImplOne")
    private GoodsClassService goodsClassService;

    @Autowired
    @Qualifier("BusinessServiceImplOne")
    private BusinessService businessService;

    @Autowired
    @Qualifier("GoodsAttributeServiceImplOne")
    private GoodsAttributeService goodsAttributeService;



    @Autowired
    @Qualifier("ServeServiceImplOne")
    private ServeService serveService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("DeviceServiceImplOne")
    private DeviceService deviceService;

    @Autowired
    @Qualifier("VersionServiceImplOne")
    private VersionService versionService;


    @Autowired
    @Qualifier("CartGoodsPriceServiceImplOne")
    private CartGoodsPriceService cartGoodsPriceService;

    @Autowired
    @Qualifier("OrderGoodsPriceServiceImplOne")
    private OrderGoodsPriceService orderGoodsPriceService;

    @Autowired
    @Qualifier("GoodsSizeServiceImplOne")
    private GoodsSizeService goodsSizeService;

    @Autowired
    @Qualifier("GoodsColorServiceImplOne")
    private GoodsColorService goodsColorService;

    @Autowired
    @Qualifier("OrderRefundApplyServiceImplOne")
    private OrderRefundApplyService orderRefundApplyService;

    @Autowired
    @Qualifier("OrderRefundServiceImplOne")
    private OrderRefundService orderRefundService;





    //立即购买提交确认
    @RequestMapping(value = "BuyNow",method = RequestMethod.POST)
    public MyResult BuyNow(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String goodsCode = jsonObject.getString("goodsCode");
            int number = jsonObject.getInteger("number");


            //字符串数组，属性code数组
            JSONArray goodsAttributeCodesArg = jsonObject.getJSONArray("goodsAttributeCodesArg");
            //字符串数组，属性code对应的值
            JSONArray goodsAttributeCodesValueArg = jsonObject.getJSONArray("goodsAttributeCodesValueArg");

            String planTimeStr = jsonObject.getString("planTime");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/BuyNow");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            Goods goods = goodsService.getByGoodsCode(goodsCode);
            GoodsDTO goodsDTO = mapper.map(goods,GoodsDTO.class);

            Business business = businessService.getByBusinessCode(goodsDTO.getBusinessCode());
            User user = userService.getByUserCode(business.getUserCode());
            goodsDTO.setBusinessName(user.getName());


            double goodsPrice = GetPrice(goodsCode,goodsAttributeCodesArg,goodsAttributeCodesValueArg);


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("goodsDTO",goodsDTO);
            jsonObject1.put("number",number);
            jsonObject1.put("goodsPrice",goodsPrice);
            jsonObject1.put("goodsAttributeCodesArg",goodsAttributeCodesArg);
            jsonObject1.put("goodsAttributeCodesValueArg",goodsAttributeCodesValueArg);
            jsonObject1.put("planTime",planTimeStr);
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/AppCare/BuyNow",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //立即购买提交
    @RequestMapping(value = "BuyNowSubmit",method = RequestMethod.POST)
    @Transactional
    public MyResult BuyNowSubmit(@RequestBody JSONObject jsonObject, HttpServletRequest request) {

        try{

            String WxPayNotify =thisUrl+":"+port+"/PayNotify/WxPayNotify";

            String AliPayNotify =thisUrl+":"+port+"/PayNotify/AliPayNotify";

            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String goodsCode = jsonObject.getString("goodsCode");
            int number = jsonObject.getInteger("number");

            //字符串数组，属性code数组
            JSONArray goodsAttributeCodesArg = jsonObject.getJSONArray("goodsAttributeCodesArg");
            //字符串数组，属性code对应的值
            JSONArray goodsAttributeCodesValueArg = jsonObject.getJSONArray("goodsAttributeCodesValueArg");


            String userAddressCode = jsonObject.getString("userAddressCode");

            //付款类型
            String payType = jsonObject.getString("payType");

            //客户端类型（app）
            String clientType = jsonObject.getString("clientType");

            //商品类型
            int goodsType = jsonObject.getInteger("goodsType");
            Date planTime = null;
            if(goodsType == 2){
                String planTimeStr = jsonObject.getString("planTime");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                planTime = sdf.parse(planTimeStr);
            }



            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/BuyNowSubmit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            User user = userService.getByUserCode(userCode);
            UserAddress userAddress = userAddressService.getByUserAddressCode(userAddressCode);
            if(userAddress == null){
                return failed("收货地址不存在");
            }

            Elderly elderly = elderlyService.getByUserCode(user.getUserCode());
            if(elderly == null){
                return failed("下单人不是老人");
            }



            Goods goods = goodsService.getByGoodsCode(goodsCode);
            String businessCode = goods.getBusinessCode();
            int orderType = goods.getGoodsType();
            //增加库存
            if(goods.getStock() > goods.getSaleCount()+number){
                goods.setSaleCount(goods.getSaleCount()+number);
                goodsService.update(goods);
            }


            double goodsPrice = GetPrice(goodsCode,goodsAttributeCodesArg,goodsAttributeCodesValueArg);
            if(goodsPrice == 0.0){
                return failed("价格错误");
            }


            //订单总金额
            double priceOrder = goodsPrice*number;

            if (payType.equals("yue")){
                if (priceOrder>elderly.getBalance()){
                    return failed("老人余额不足");
                }
            }





            //支付交易号
            String tradeno = "sn"+ getTimeStrSSS()+ new Random().nextInt(100000);
            //订单流水号
            String serialNumber = getTimeStrSSS() +user.getPhone()+ new Random().nextInt(100000);



            //订单的支付交易号
            OrderTradeno orderTradeno = new OrderTradeno();
            orderTradeno.setOrderTradenoCode(UUID.randomUUID().toString());
            orderTradeno.setUserCode(userCode);
            orderTradeno.setTradeNo(tradeno);
            orderTradeno.setPrice(priceOrder);
            orderTradeno.setCreateTime(new Date());
            orderTradeno.setStatus(0);
            orderTradeno.setPayType(payType);
            orderTradeno.setClientType(clientType);
            orderTradenoService.insert(orderTradeno);



            //订单
            Order order = new Order();
            order.setOrderCode(UUID.randomUUID().toString());
            order.setUserCode(userCode);
            order.setPrice(priceOrder);
            order.setCreateTime(new Date());
            order.setReceiveName(userAddress.getReceiveName());
            order.setReceivePhone(userAddress.getReceivePhone());
            order.setReceiveArea(userAddress.getAreaName());
            order.setReceiveAreaPath(userAddress.getAreaNamePath());
            order.setReceiveAddress(userAddress.getReceiveAddress());
            order.setGovernmentCode(governmentCode);
            order.setCashierElderlyCode(emptyUUID);
            orderService.insert(order);





            Business business = businessService.getByBusinessCode(businessCode);
            User userBusiness = userService.getByUserCode(business.getUserCode());

            //订单的商家
            OrderBusiness orderBusiness = new OrderBusiness();
            orderBusiness.setOrderBusinessCode(UUID.randomUUID().toString());
            orderBusiness.setOrderCode(order.getOrderCode());
            orderBusiness.setBusinessCode(business.getBusinessCode());
            orderBusiness.setBusinessName(userBusiness.getName());
            orderBusiness.setPrice(priceOrder);
            orderBusiness.setCreateTime(new Date());
            orderBusiness.setOrderType(goods.getGoodsType());
            if(orderType == 1){
                orderBusiness.setStatus(0);
            }
            if(orderType == 2){
                orderBusiness.setStatus(10);
            }
            orderBusiness.setSerialNumber(serialNumber);
            orderBusiness.setPayType(payType);

            orderBusiness.setOrderFrom("android");

            orderBusinessService.insert(orderBusiness);


            //商家的订单与支付交易号关联
            OrderBusinessTradenoRel orderBusinessTradenoRel = new OrderBusinessTradenoRel();
            orderBusinessTradenoRel.setOrderBusinessTradeNoRelCode(UUID.randomUUID().toString());
            orderBusinessTradenoRel.setTradeNo(tradeno);
            orderBusinessTradenoRel.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
            orderBusinessTradenoRelService.insert(orderBusinessTradenoRel);



            //订单的商品
            OrderGoods orderGoods = new OrderGoods();
            String orderGoodsCode=UUID.randomUUID().toString();
            orderGoods.setOrderGoodsCode(orderGoodsCode);
            orderGoods.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
            orderGoods.setGoodsCode(goodsCode);
            orderGoods.setNumber(number);
            orderGoods.setGoodsName(goods.getName());
            orderGoods.setGoodsPrice(goodsPrice);
            orderGoods.setGoodsImg(goods.getHead1());
            orderGoods.setPlanTime(planTime);
            orderGoodsService.insert(orderGoods);




            //订单商品的属性
            OrderGoodsPrice orderGoodsPrice = new OrderGoodsPrice();
            orderGoodsPrice.setOrderGoodsPriceCode(UUID.randomUUID().toString());
            orderGoodsPrice.setOrderGoodsCode(orderGoodsCode);
            orderGoodsPrice.setPrice(goodsPrice);

            String goodsSizeCode = getValueByKey("goodsSizeCode",goodsAttributeCodesArg,
                    goodsAttributeCodesValueArg);
            String goodsSizeName = "";
            if(!goodsSizeCode.equals("")){
                goodsSizeName = goodsSizeService.getByGoodsSizeCode(goodsSizeCode).getName();
            }
            orderGoodsPrice.setGoodsSizeCode(goodsSizeCode);
            orderGoodsPrice.setGoodsSizeName(goodsSizeName);

            String goodsColorCode = getValueByKey("goodsColorCode",goodsAttributeCodesArg,
                    goodsAttributeCodesValueArg);
            String goodsColorName = "";
            if(!goodsColorCode.equals("")){
                goodsColorName = goodsColorService.getByGoodsColorCode(goodsColorCode).getName();
            }
            orderGoodsPrice.setGoodsColorCode(goodsColorCode);
            orderGoodsPrice.setGoodsColorName(goodsColorName);
            orderGoodsPriceService.insert(orderGoodsPrice);





            String appUseStr = "";
            Map wxPayMap=null;
            if(payType.equals("yue")){

                writePushFile(business,orderType);

                elderly.setBalance(elderly.getBalance() - priceOrder);
                elderlyService.update(elderly);




                //修改总订单的支付状态
                OrderTradeno orderTradeno1 = orderTradenoService.getByTradeno(tradeno);
                if(orderTradeno1.getStatus() == 0){
                    orderTradeno1.setStatus(1);
                    orderTradenoService.update(orderTradeno1);
                }


                //交易号关联的订单
                List<OrderBusinessTradenoRel> listOrderBusinessTradeoRel =
                        orderBusinessTradenoRelService.getListByTradeno(tradeno);

                //修改订单的状态
                for(OrderBusinessTradenoRel orderBusinessTradenoRel2:listOrderBusinessTradeoRel){
                    OrderBusiness orderBusiness2 =
                            orderBusinessService.getByOrderBusinessCode(orderBusinessTradenoRel2.getOrderBusinessCode());
                    if (orderBusiness2.getOrderType()==1){
                        if(orderBusiness2.getStatus() == 0){
                            orderBusiness2.setStatus(1);
                            orderBusiness2.setPayTime(new Date());
                            orderBusinessService.update(orderBusiness2);
                        }
                    }
                    if (orderBusiness2.getOrderType()==2){
                        if(orderBusiness2.getStatus() == 10){
                            orderBusiness2.setStatus(11);
                            orderBusiness2.setPayTime(new Date());
                            orderBusinessService.update(orderBusiness2);
                        }
                    }

                }



            }

            if(payType.equals("wx")){
                wxPayMap = WechatPayUtils.appOrders(getRemoteHost(request), tradeno, priceOrder,WxPayNotify);
            }

            if(payType.equals("ali")){
                appUseStr = AliPayUtils.aliPay(tradeno,priceOrder+"",goods.getName(),goods.getNameMore(),AliPayNotify);
            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("payType",payType);
            jsonObject1.put("appUseStr",appUseStr);
            jsonObject1.put("wxPayMap",wxPayMap);
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCare/BuyNowSubmit!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    //加入购物车
    @RequestMapping(value = "CartAdd",method = RequestMethod.POST)
    @Transactional
    public MyResult CartAdd(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String goodsCode = jsonObject.getString("goodsCode");
            int number = jsonObject.getInteger("number");


            //字符串数组，属性code数组
            JSONArray goodsAttributeCodesArg = jsonObject.getJSONArray("goodsAttributeCodesArg");
            //字符串数组，属性code对应的值
            JSONArray goodsAttributeCodesValueArg = jsonObject.getJSONArray("goodsAttributeCodesValueArg");



            //商品类型
            int goodsType = jsonObject.getInteger("goodsType");
            Date planTime = null;
            if(goodsType == 2){
                String planTimeStr = jsonObject.getString("planTime");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                planTime = sdf.parse(planTimeStr);
            }


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/CartAdd");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            double goodsPrice = GetPrice(goodsCode,goodsAttributeCodesArg,goodsAttributeCodesValueArg);


            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            Goods goods = goodsService.getByGoodsCode(goodsCode);
            String businessCode = goods.getBusinessCode();



            Cart cart = cartService.getByUserCodeGovernmentCode(userCode,governmentCode);
            if(cart == null){
                cart = new Cart();
                cart.setCartCode(UUID.randomUUID().toString());
                cart.setUserCode(userCode);
                cart.setGovernmentCode(governmentCode);
                cartService.insert(cart);
            }




            //check goods in cart or not
            Boolean isExist = false;
            List<CartBusiness> listCartBusiness = cartBusinessService.getListByCartCode(cart.getCartCode());
            for(CartBusiness cartBusiness : listCartBusiness){
                List<CartGoods> listCartGoods = cartGoodsService.getListByCartBusinessCode(cartBusiness.getCartBusinessCode());
                for(CartGoods cartGoods : listCartGoods){
                    if(cartGoods.getGoodsCode().equals(goodsCode)){

                        Boolean b = hadCartGoodsPriceExist(cartGoods.getCartGoodsCode(),goodsAttributeCodesArg,
                                goodsAttributeCodesValueArg);
                        if(b){
                            isExist = true;
                        }

                        //isExist = true;
                        break;
                    }
                }
            }



            if(!isExist){


                //cart business
                CartBusiness cartBusinessSelected = null;
                //普通商品合并订单
                if(goods.getGoodsType() == 1){
                    for(CartBusiness cartBusiness : listCartBusiness){
                        if(cartBusiness.getBusinessCode().equals(businessCode) && cartBusiness.getGoodsType() == 1 ){
                            cartBusinessSelected = cartBusiness;
                            break;
                        }
                    }
                }
                if(cartBusinessSelected == null){
                    cartBusinessSelected = new CartBusiness();
                    cartBusinessSelected.setCartBusinessCode(UUID.randomUUID().toString());
                    cartBusinessSelected.setCartCode(cart.getCartCode());
                    cartBusinessSelected.setBusinessCode(businessCode);
                    cartBusinessSelected.setPrice(goodsPrice*number);
                    cartBusinessSelected.setGoodsType(goods.getGoodsType());
                    cartBusinessService.insert(cartBusinessSelected);
                }




                //cart goods
                CartGoods cartGoodsSelected = null;
                List<CartGoods> listCartGoods = cartGoodsService.getListByCartBusinessCode(cartBusinessSelected.getCartBusinessCode());
                for(CartGoods cartGoods : listCartGoods){
                    if(cartGoods.getGoodsCode().equals(goodsCode)){

                        Boolean b = hadCartGoodsPriceExist(cartGoods.getCartGoodsCode(),goodsAttributeCodesArg,
                                goodsAttributeCodesValueArg);
                        if(b){
                            cartGoodsSelected = cartGoods;
                        }

                        //cartGoodsSelected = cartGoods;
                        break;
                    }
                }
                if(cartGoodsSelected == null){
                    cartGoodsSelected = new CartGoods();
                    cartGoodsSelected.setCartGoodsCode(UUID.randomUUID().toString());
                    cartGoodsSelected.setCartBusinessCode(cartBusinessSelected.getCartBusinessCode());
                    cartGoodsSelected.setGoodsCode(goodsCode);
                    cartGoodsSelected.setNumber(number);
                    cartGoodsSelected.setCreateTime(new Date());
                    cartGoodsSelected.setPlanTime(planTime);
                    cartGoodsService.insert(cartGoodsSelected);


                    CartGoodsPrice cartGoodsPrice = new CartGoodsPrice();
                    cartGoodsPrice.setCartGoodsPriceCode(UUID.randomUUID().toString());
                    cartGoodsPrice.setCartGoodsCode(cartGoodsSelected.getCartGoodsCode());
                    cartGoodsPrice.setPrice(goodsPrice);
                    cartGoodsPrice.setGoodsSizeCode(getValueByKey("goodsSizeCode",goodsAttributeCodesArg,
                            goodsAttributeCodesValueArg));
                    cartGoodsPrice.setGoodsColorCode(getValueByKey("goodsColorCode",goodsAttributeCodesArg,
                            goodsAttributeCodesValueArg));
                    cartGoodsPriceService.insert(cartGoodsPrice);


                }



            }








            //update price
            double priceCart = 0;
            List<CartBusiness> listCartBusiness2 = cartBusinessService.getListByCartCode(cart.getCartCode());
            for(CartBusiness cartBusiness : listCartBusiness2){

                double priceBusiness = 0;
                List<CartGoods> listCartGoods2 = cartGoodsService.getListByCartBusinessCode(cartBusiness.getCartBusinessCode());
                for(CartGoods cartGoods:listCartGoods2){
                    CartGoodsPrice cartGoodsPrice = cartGoodsPriceService.getByCartGoodsCode(cartGoods.getCartGoodsCode());
                    priceBusiness += cartGoodsPrice.getPrice() * cartGoods.getNumber();
                }
                cartBusiness.setPrice(priceBusiness);
                cartBusinessService.update(cartBusiness);

                priceCart += priceBusiness;
            }
            cart.setPrice(priceCart);
            cartService.update(cart);





            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCare/CartAdd!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    //购物车
    @RequestMapping(value = "Cart",method = RequestMethod.POST)
    public MyResult Cart(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/Cart");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            //setGovernmentCode(userCode);
            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            Cart cart = cartService.getByUserCodeGovernmentCode(userCode,governmentCode);
            CartDTO cartDTO = new CartDTO();
            if(cart != null){
                cartDTO = mapper.map(cart, CartDTO.class);


                List<CartBusiness> listCartBusiness = cartBusinessService.getListByCartCode(cartDTO.getCartCode());
                List<CartBusinessDTO> listCartBusinessDTO = new ArrayList<>();
                for(CartBusiness cartBusiness:listCartBusiness){
                    CartBusinessDTO cartBusinessDTO = mapper.map(cartBusiness, CartBusinessDTO.class);
                    listCartBusinessDTO.add(cartBusinessDTO);
                }

                for(CartBusinessDTO cartBusinessDTO :listCartBusinessDTO){

                    Business business = businessService.getByBusinessCode(cartBusinessDTO.getBusinessCode());
                    User user = userService.getByUserCode(business.getUserCode());
                    cartBusinessDTO.setBusinessName(user.getName());


                    List<CartGoods> listCartGoods =
                            cartGoodsService.getListByCartBusinessCode(cartBusinessDTO.getCartBusinessCode());
                    List<CartGoodsDTO> listCartGoodsDTO = new ArrayList<>();
                    for(CartGoods cartGoods:listCartGoods){
                        CartGoodsDTO cartGoodsDTO = mapper.map(cartGoods,CartGoodsDTO.class);
                        listCartGoodsDTO.add(cartGoodsDTO);
                    }

                    for(CartGoodsDTO cartGoodsDTO:listCartGoodsDTO){
                        Goods goods = goodsService.getByGoodsCode(cartGoodsDTO.getGoodsCode());
                        cartGoodsDTO.setGoodsName(goods.getName());
                        cartGoodsDTO.setGoodsHead(goods.getHead1());

//                        CartGoodsPrice cartGoodsPrice =
//                                cartGoodsPriceService.getByCartGoodsCode(cartGoodsDTO.getCartGoodsCode());
//                        cartGoodsDTO.setCartGoodsPriceDTO(mapper.map(cartGoodsPrice, CartGoodsPriceDTO.class));

                        CartGoodsPrice cartGoodsPrice =
                                cartGoodsPriceService.getByCartGoodsCode(cartGoodsDTO.getCartGoodsCode());
                        CartGoodsPriceDTO cartGoodsPriceDTO = mapper.map(cartGoodsPrice, CartGoodsPriceDTO.class);
                        if(cartGoodsPrice.getGoodsSizeCode()!=null && !cartGoodsPrice.getGoodsSizeCode().equals("")){
                            GoodsSize goodsSize =goodsSizeService.getByGoodsSizeCode(cartGoodsPrice.getGoodsSizeCode());
                            cartGoodsPriceDTO.setGoodsSizeName(goodsSize.getName());
                        }
                        if(cartGoodsPrice.getGoodsColorCode()!=null && !cartGoodsPrice.getGoodsColorCode().equals("")){
                            GoodsColor goodsColor =goodsColorService.getByGoodsColorCode(cartGoodsPrice.getGoodsColorCode());
                            cartGoodsPriceDTO.setGoodsColorName(goodsColor.getName());
                        }
                        cartGoodsDTO.setCartGoodsPriceDTO(cartGoodsPriceDTO);


                    }
                    cartBusinessDTO.setListCartGoodsDTO(listCartGoodsDTO);

                }

                cartDTO.setListCartBusinessDTO(listCartBusinessDTO);
            }


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("cartDTO",cartDTO);
            return success(jsonObject1);

        }catch (Exception e){
            insertError("/AppCare/Cart",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //修改购物车中商品数量
    @RequestMapping(value = "CartNumberChange",method = RequestMethod.POST)
    public MyResult CartNumberChange(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String cartGoodsCode = jsonObject.getString("cartGoodsCode");
            int number = jsonObject.getInteger("number");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/CartNumberChange");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            //setGovernmentCode(userCode);
            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            //cart goods
            CartGoods cartGoodsSelected = cartGoodsService.getByCartGoodsCode(cartGoodsCode);

            Goods goods = goodsService.getByGoodsCode(cartGoodsSelected.getGoodsCode());
            if(goods.getStock() < number + goods.getSaleCount()){
                //return failed("库存不足");
            }
            cartGoodsSelected.setNumber(number);
            cartGoodsService.update(cartGoodsSelected);



            Cart cart = cartService.getByUserCodeGovernmentCode(userCode,governmentCode);

            //update price
            double priceCart = 0;
            List<CartBusiness> listCartBusiness2 = cartBusinessService.getListByCartCode(cart.getCartCode());
            for(CartBusiness cartBusiness : listCartBusiness2){

                double priceBusiness = 0;
                List<CartGoods> listCartGoods2 = cartGoodsService.getListByCartBusinessCode(cartBusiness.getCartBusinessCode());
                for(CartGoods cartGoods:listCartGoods2){
                    //Goods goods1 = goodsService.getByGoodsCode(cartGoods.getGoodsCode());
                    //priceBusiness += goods1.getPriceSale() * cartGoods.getNumber();

                    CartGoodsPrice cartGoodsPrice = cartGoodsPriceService.getByCartGoodsCode(cartGoods.getCartGoodsCode());
                    priceBusiness += cartGoodsPrice.getPrice() * cartGoods.getNumber();

                }
                cartBusiness.setPrice(priceBusiness);
                cartBusinessService.update(cartBusiness);


                priceCart += priceBusiness;
            }
            cart.setPrice(priceCart);
            cartService.update(cart);



            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);

        }catch (Exception e){
            insertError("/AppCare/CartNumberChange",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //修改购物车中服务类商品的预约时间
    @RequestMapping(value = "CartPlanTimeChange",method = RequestMethod.POST)
    public MyResult CartPlanTimeChange(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String cartGoodsCode = jsonObject.getString("cartGoodsCode");
            String planTimeStr = jsonObject.getString("planTime");

//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/CartPlanTimeChange");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            //setGovernmentCode(userCode);
            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date planTime = sdf.parse(planTimeStr);


            //cart goods
            CartGoods cartGoodsSelected = cartGoodsService.getByCartGoodsCode(cartGoodsCode);
            cartGoodsSelected.setPlanTime(planTime);
            cartGoodsService.update(cartGoodsSelected);


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);

        }catch (Exception e){
            insertError("/AppCare/CartPlanTimeChange",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //删除购物车中商品
    @RequestMapping(value = "CartItemDelete",method = RequestMethod.POST)
    @Transactional
    public MyResult CartItemDelete(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String cartGoodsCode = jsonObject.getString("cartGoodsCode");

//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/CartItemDelete");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            //setGovernmentCode(userCode);
            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            CartGoods cartGoods = cartGoodsService.getByCartGoodsCode(cartGoodsCode);
            cartGoodsService.delete(cartGoodsCode);
            CartGoodsPrice cartGoodsPrice = cartGoodsPriceService.getByCartGoodsCode(cartGoods.getCartGoodsCode());
            cartGoodsPriceService.delete(cartGoodsPrice.getCartGoodsPriceCode());


            List<CartGoods> listCarGoods = cartGoodsService.getListByCartBusinessCode(cartGoods.getCartBusinessCode());
            if(listCarGoods.size() == 0){
                cartBusinessService.delete(cartGoods.getCartBusinessCode());
            }




            Cart cart = cartService.getByUserCodeGovernmentCode(userCode,governmentCode);

            //update price
            double priceCart = 0;
            List<CartBusiness> listCartBusiness2 = cartBusinessService.getListByCartCode(cart.getCartCode());
            for(CartBusiness cartBusiness : listCartBusiness2){

                double priceBusiness = 0;
                List<CartGoods> listCartGoods2 = cartGoodsService.getListByCartBusinessCode(cartBusiness.getCartBusinessCode());
                for(CartGoods cartGoods2:listCartGoods2){
                    CartGoodsPrice cartGoodsPrice2 =
                            cartGoodsPriceService.getByCartGoodsCode(cartGoods2.getCartGoodsCode());
                    priceBusiness += cartGoodsPrice2.getPrice() * cartGoods2.getNumber();
                }
                cartBusiness.setPrice(priceBusiness);
                cartBusinessService.update(cartBusiness);

                priceCart += priceBusiness;
            }
            cart.setPrice(priceCart);
            cartService.update(cart);





            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCare/CartItemDelete!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    //购物车提交确认
    @RequestMapping(value = "CartConfirm",method = RequestMethod.POST)
    public MyResult CartConfirm(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/CartConfirm");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            //setGovernmentCode(userCode);
            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();

            Cart cart = cartService.getByUserCodeGovernmentCode(userCode,governmentCode);
            CartDTO cartDTO = mapper.map(cart,CartDTO.class);


            List<CartBusiness> listCartBusiness = cartBusinessService.getListByCartCode(cart.getCartCode());
            List<CartBusinessDTO> listCartBusinessDTO = new ArrayList<>();
            for(CartBusiness cartBusiness:listCartBusiness){
                CartBusinessDTO cartBusinessDTO = mapper.map(cartBusiness, CartBusinessDTO.class);
                listCartBusinessDTO.add(cartBusinessDTO);
            }


            for(CartBusinessDTO cartBusinessDTO :listCartBusinessDTO){

                Business business = businessService.getByBusinessCode(cartBusinessDTO.getBusinessCode());
                User user = userService.getByUserCode(business.getUserCode());
                cartBusinessDTO.setBusinessName(user.getName());



                List<CartGoods> listCartGoods =
                        cartGoodsService.getListByCartBusinessCode(cartBusinessDTO.getCartBusinessCode());
                List<CartGoodsDTO> listCartGoodsDTO = new ArrayList<>();
                for(CartGoods cartGoods:listCartGoods){
                    CartGoodsDTO cartGoodsDTO = mapper.map(cartGoods,CartGoodsDTO.class);
                    listCartGoodsDTO.add(cartGoodsDTO);
                }

                for(CartGoodsDTO cartGoodsDTO:listCartGoodsDTO){
                    Goods goods = goodsService.getByGoodsCode(cartGoodsDTO.getGoodsCode());
                    cartGoodsDTO.setGoodsName(goods.getName());
                    cartGoodsDTO.setGoodsHead(goods.getHead1());

                    CartGoodsPrice cartGoodsPrice =
                            cartGoodsPriceService.getByCartGoodsCode(cartGoodsDTO.getCartGoodsCode());
                    cartGoodsDTO.setCartGoodsPriceDTO(mapper.map(cartGoodsPrice, CartGoodsPriceDTO.class));
                }
                cartBusinessDTO.setListCartGoodsDTO(listCartGoodsDTO);
            }

            cartDTO.setListCartBusinessDTO(listCartBusinessDTO);


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("cartDTO",cartDTO);
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/AppCare/CartConfirm",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //购物车提交
    @RequestMapping(value = "CartSubmit",method = RequestMethod.POST)
    @Transactional
    public MyResult CartSubmit(@RequestBody JSONObject jsonObject,HttpServletRequest request) {

        try{

            String WxPayNotify =thisUrl+":"+port+"/PayNotify/WxPayNotify";

            String AliPayNotify =thisUrl+":"+port+"/PayNotify/AliPayNotify";

            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String userAddressCode = jsonObject.getString("userAddressCode");
            String payType = jsonObject.getString("payType");
            String clientType = jsonObject.getString("clientType");

//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/CartSubmit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            //setGovernmentCode(userCode);
            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            User user = userService.getByUserCode(userCode);
            UserAddress userAddress = userAddressService.getByUserAddressCode(userAddressCode);
            if(userAddress == null){
                return failed("收货地址不存在");
            }


            Elderly elderly = elderlyService.getByUserCode(user.getUserCode());
            if(elderly == null){
                return failed("下单人不是老人");
            }



            //支付交易号
            String tradeno = "sn"+ getTimeStrSSS()+ new Random().nextInt(100000);



            Cart cart = cartService.getByUserCodeGovernmentCode(userCode,governmentCode);
            if(cart == null){
                return failed("购物车为空");
            }
            if (payType.equals("yue")){
                if(cart.getPrice()>elderly.getBalance()){
                    return failed("老人余额不足");
                }
            }
            cartService.delete(cart.getCartCode());


            //订单的支付交易号
            OrderTradeno orderTradeno = new OrderTradeno();
            orderTradeno.setOrderTradenoCode(UUID.randomUUID().toString());
            orderTradeno.setUserCode(userCode);
            orderTradeno.setTradeNo(tradeno);
            orderTradeno.setPrice(cart.getPrice());
            orderTradeno.setCreateTime(new Date());
            orderTradeno.setStatus(0);
            orderTradeno.setPayType(payType);
            orderTradeno.setClientType(clientType);
            orderTradenoService.insert(orderTradeno);


            //订单
            Order order = new Order();
            order.setOrderCode(UUID.randomUUID().toString());
            order.setUserCode(userCode);
            order.setPrice(cart.getPrice());
            order.setCreateTime(new Date());
            order.setReceiveName(userAddress.getReceiveName());
            order.setReceivePhone(userAddress.getReceivePhone());
            order.setReceiveArea(userAddress.getAreaName());
            order.setReceiveAreaPath(userAddress.getAreaNamePath());
            order.setReceiveAddress(userAddress.getReceiveAddress());
            order.setGovernmentCode(governmentCode);
            order.setCashierElderlyCode(emptyUUID);
            orderService.insert(order);






            List<CartBusiness> listCartBusiness = cartBusinessService.getListByCartCode(cart.getCartCode());

            String goodsNames="";
            String goodsNameMores="";

            for(CartBusiness cartBusiness : listCartBusiness){
                cartBusinessService.delete(cartBusiness.getCartBusinessCode());


                Business business = businessService.getByBusinessCode(cartBusiness.getBusinessCode());
                User userBusiness = userService.getByUserCode(business.getUserCode());


                //订单流水号
                String serialNumber = getTimeStrSSS() +user.getPhone()+ new Random().nextInt(100000);

                //订单的商家
                OrderBusiness orderBusiness = new OrderBusiness();
                orderBusiness.setOrderBusinessCode(UUID.randomUUID().toString());
                orderBusiness.setOrderCode(order.getOrderCode());
                orderBusiness.setBusinessCode(cartBusiness.getBusinessCode());
                orderBusiness.setBusinessName(userBusiness.getName());
                orderBusiness.setPrice(cartBusiness.getPrice());
                orderBusiness.setCreateTime(new Date());
                orderBusiness.setOrderType(cartBusiness.getGoodsType());
                if(cartBusiness.getGoodsType() == 1){
                    orderBusiness.setStatus(0);
                }
                if(cartBusiness.getGoodsType() == 2){
                    orderBusiness.setStatus(10);
                }
                orderBusiness.setSerialNumber(serialNumber);
                orderBusiness.setPayType(payType);

                orderBusiness.setOrderFrom("android");

                orderBusinessService.insert(orderBusiness);


                //商家的订单与支付交易号关联
                OrderBusinessTradenoRel orderBusinessTradenoRel = new OrderBusinessTradenoRel();
                orderBusinessTradenoRel.setOrderBusinessTradeNoRelCode(UUID.randomUUID().toString());
                orderBusinessTradenoRel.setTradeNo(tradeno);
                orderBusinessTradenoRel.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                orderBusinessTradenoRelService.insert(orderBusinessTradenoRel);


                int orderType = 0;
                double priceOrder = orderBusiness.getPrice();

                List<CartGoods> listCartGoods =
                        cartGoodsService.getListByCartBusinessCode(cartBusiness.getCartBusinessCode());
                for(CartGoods cartGoods : listCartGoods){
                    cartGoodsService.delete(cartGoods.getCartGoodsCode());

                    Goods goods = goodsService.getByGoodsCode(cartGoods.getGoodsCode());
                    orderType = goods.getGoodsType();

                    goodsNames+=goods.getName()+",";
                    goodsNameMores+=goods.getNameMore()+",";

                    CartGoodsPrice cartGoodsPrice =
                            cartGoodsPriceService.getByCartGoodsCode(cartGoods.getCartGoodsCode());



                    //订单的商品
                    OrderGoods orderGoods = new OrderGoods();
                    orderGoods.setOrderGoodsCode(UUID.randomUUID().toString());
                    orderGoods.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                    orderGoods.setGoodsCode(cartGoods.getGoodsCode());
                    orderGoods.setNumber(cartGoods.getNumber());
                    orderGoods.setPlanTime(cartGoods.getPlanTime());
                    orderGoods.setGoodsName(goods.getName());
                    orderGoods.setGoodsPrice(cartGoodsPrice.getPrice());
                    orderGoods.setGoodsImg(goods.getHead1());
                    orderGoodsService.insert(orderGoods);



                    //订单商品的属性
                    OrderGoodsPrice orderGoodsPrice = new OrderGoodsPrice();
                    orderGoodsPrice.setOrderGoodsPriceCode(UUID.randomUUID().toString());
                    orderGoodsPrice.setOrderGoodsCode(orderGoods.getOrderGoodsCode());
                    orderGoodsPrice.setPrice(cartGoodsPrice.getPrice());

                    String goodsSizeCode = cartGoodsPrice.getGoodsSizeCode();
                    String goodsSizeName = "";
                    if(!goodsSizeCode.equals("")){
                        goodsSizeName = goodsSizeService.getByGoodsSizeCode(goodsSizeCode).getName();
                    }
                    orderGoodsPrice.setGoodsSizeCode(goodsSizeCode);
                    orderGoodsPrice.setGoodsSizeName(goodsSizeName);

                    String goodsColorCode =cartGoodsPrice.getGoodsColorCode();
                    String goodsColorName = "";
                    if(!goodsColorCode.equals("")){
                        goodsColorName = goodsColorService.getByGoodsColorCode(goodsColorCode).getName();
                    }
                    orderGoodsPrice.setGoodsColorCode(goodsColorCode);
                    orderGoodsPrice.setGoodsColorName(goodsColorName);
                    orderGoodsPriceService.insert(orderGoodsPrice);




                    //增加库存
                    if(goods.getStock() > goods.getSaleCount()+cartGoods.getNumber()){
                        goods.setSaleCount(goods.getSaleCount()+cartGoods.getNumber());
                        goodsService.update(goods);
                    }

                }



                if(payType.equals("yue")){
                    writePushFile(business,orderType);

                    elderly.setBalance(elderly.getBalance() - priceOrder);
                    elderlyService.update(elderly);
                }


            }

            goodsNames=goodsNames.substring(0,goodsNames.length()-1);
            goodsNameMores=goodsNameMores.substring(0,goodsNameMores.length()-1);

            if (goodsNames.length()>256){
                goodsNames=goodsNames.substring(0,256);
            }
            if (goodsNameMores.length()>1000){
                goodsNames=goodsNameMores.substring(0,1000);
            }



            String appUseStr = "";
            Map wxPayMap=null;

            if(payType.equals("yue")){

                //修改总订单的支付状态
                OrderTradeno orderTradeno1 = orderTradenoService.getByTradeno(tradeno);
                if(orderTradeno1.getStatus() == 0){
                    orderTradeno1.setStatus(1);
                    orderTradenoService.update(orderTradeno1);
                }


                //交易号关联的订单
                List<OrderBusinessTradenoRel> listOrderBusinessTradeoRel =
                        orderBusinessTradenoRelService.getListByTradeno(tradeno);

                //修改订单的状态
                for(OrderBusinessTradenoRel orderBusinessTradenoRel2:listOrderBusinessTradeoRel){
                    OrderBusiness orderBusiness2 =
                            orderBusinessService.getByOrderBusinessCode(orderBusinessTradenoRel2.getOrderBusinessCode());
                    if (orderBusiness2.getOrderType()==1){
                        if(orderBusiness2.getStatus() == 0){
                            orderBusiness2.setStatus(1);
                            orderBusiness2.setPayTime(new Date());
                            orderBusinessService.update(orderBusiness2);
                        }
                    }
                    if (orderBusiness2.getOrderType()==2){
                        if(orderBusiness2.getStatus() == 10){
                            orderBusiness2.setStatus(11);
                            orderBusiness2.setPayTime(new Date());
                            orderBusinessService.update(orderBusiness2);
                        }
                    }
                }

            }

            if(payType.equals("wx")){
                wxPayMap = WechatPayUtils.appOrders(getRemoteHost(request), tradeno, cart.getPrice(),WxPayNotify);
            }
            if(payType.equals("ali")){
                appUseStr = AliPayUtils.aliPay(tradeno,cart.getPrice()+"",goodsNames,goodsNameMores,AliPayNotify);
            }



            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("payType",payType);
            jsonObject1.put("appUseStr",appUseStr);
            jsonObject1.put("wxPayMap",wxPayMap);
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCare/CartSubmit!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    //订单中去付款
    @RequestMapping(value = "OrderBusinessToPay",method = RequestMethod.POST)
    @Transactional
    public MyResult OrderBusinessToPay(@RequestBody JSONObject jsonObject,HttpServletRequest request) {

        try{

            String WxPayNotify =thisUrl+":"+port+"/PayNotify/WxPayNotify";

            String AliPayNotify =thisUrl+":"+port+"/PayNotify/AliPayNotify";

            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String orderBusinessCode = jsonObject.getString("orderBusinessCode");

            String payType = jsonObject.getString("payType");
            String clientType = jsonObject.getString("clientType");

//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/OrderBusinessToPay");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);

            OrderBusiness orderBusiness = orderBusinessService.getByOrderBusinessCode(orderBusinessCode);
            List<OrderGoodsDTO> orderGoodsDTOList = orderGoodsService.getListByOrderBusinessCode(orderBusinessCode);

            String goodsNames="";
            String goodsNameMores="";
            for (OrderGoodsDTO orderGoodsDTO : orderGoodsDTOList) {
                GoodsDTO goodsDTO = goodsService.getByGoodsCode(orderGoodsDTO.getGoodsCode());
                goodsNames+=goodsDTO.getName()+",";
                goodsNameMores+=goodsDTO.getNameMore()+",";
            }
            goodsNames=goodsNames.substring(0,goodsNames.length()-1);
            goodsNameMores=goodsNameMores.substring(0,goodsNameMores.length()-1);

            //订单总金额
            double priceOrder = orderBusiness.getPrice();






            //支付交易号
            String tradeno = "sn"+ getTimeStrSSS()+ new Random().nextInt(100000);



            //订单的支付交易号
            OrderTradeno orderTradeno = new OrderTradeno();
            orderTradeno.setOrderTradenoCode(UUID.randomUUID().toString());
            orderTradeno.setUserCode(userCode);
            orderTradeno.setTradeNo(tradeno);
            orderTradeno.setPrice(priceOrder);
            orderTradeno.setCreateTime(new Date());
            orderTradeno.setStatus(0);
            orderTradeno.setPayType(payType);
            orderTradeno.setClientType(clientType);
            orderTradenoService.insert(orderTradeno);



            //商家的订单与支付交易号关联
            OrderBusinessTradenoRel orderBusinessTradenoRel = new OrderBusinessTradenoRel();
            orderBusinessTradenoRel.setOrderBusinessTradeNoRelCode(UUID.randomUUID().toString());
            orderBusinessTradenoRel.setTradeNo(tradeno);
            orderBusinessTradenoRel.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
            orderBusinessTradenoRelService.insert(orderBusinessTradenoRel);




            orderBusiness.setPayType(payType);
            orderBusinessService.update(orderBusiness);



            String appUseStr = "";
            Map wxPayMap=null;

            if(payType.equals("yue")){

                Business business = businessService.getByBusinessCode(orderBusiness.getBusinessCode());
                Order order = orderService.getByOrderCode(orderBusiness.getOrderCode());
                Elderly elderly = elderlyService.getByUserCode(order.getUserCode());
                if (priceOrder>elderly.getBalance()){
                    return failed("老人余额不足");
                }


                writePushFile(business,orderBusiness.getOrderType());


                elderly.setBalance(elderly.getBalance() - priceOrder);
                elderlyService.update(elderly);




                //修改总订单的支付状态
                OrderTradeno orderTradeno1 = orderTradenoService.getByTradeno(tradeno);
                if(orderTradeno1.getStatus() == 0){
                    orderTradeno1.setStatus(1);
                    orderTradenoService.update(orderTradeno1);
                }


                //交易号关联的订单
                List<OrderBusinessTradenoRel> listOrderBusinessTradeoRel =
                        orderBusinessTradenoRelService.getListByTradeno(tradeno);

                //修改订单的状态
                for(OrderBusinessTradenoRel orderBusinessTradenoRel2:listOrderBusinessTradeoRel){
                    OrderBusiness orderBusiness2 =
                            orderBusinessService.getByOrderBusinessCode(orderBusinessTradenoRel2.getOrderBusinessCode());
                    if (orderBusiness2.getOrderType()==1){
                        if(orderBusiness2.getStatus() == 0){
                            orderBusiness2.setStatus(1);
                            orderBusiness2.setPayTime(new Date());
                            orderBusinessService.update(orderBusiness2);
                        }
                    }
                    if (orderBusiness2.getOrderType()==2){
                        if(orderBusiness2.getStatus() == 10){
                            orderBusiness2.setStatus(11);
                            orderBusiness2.setPayTime(new Date());
                            orderBusinessService.update(orderBusiness2);
                        }
                    }

                }



            }

            if(payType.equals("wx")){
                wxPayMap = WechatPayUtils.appOrders(getRemoteHost(request), tradeno, priceOrder,WxPayNotify);
            }

            if(payType.equals("ali")){
                appUseStr = AliPayUtils.aliPay(tradeno,priceOrder+"",goodsNames,goodsNameMores,AliPayNotify);
            }



            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("payType",payType);
            jsonObject1.put("appUseStr",appUseStr);
            jsonObject1.put("wxPayMap",wxPayMap);
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCare/OrderBusinessToPay!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    //取消未付款的订单
    @RequestMapping(value = "OrderBusinessToCancel",method = RequestMethod.POST)
    @Transactional
    public MyResult OrderBusinessToCancel(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String orderBusinessCode = jsonObject.getString("orderBusinessCode");


//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/OrderBusinessToCancel");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            OrderBusiness orderBusiness = orderBusinessService.getByOrderBusinessCode(orderBusinessCode);

            if(orderBusiness == null){
                return failed("订单不存在");
            }

            if(orderBusiness.getOrderType() == 1){
                if(orderBusiness.getStatus() == 0){
                    orderBusiness.setStatus(5);
                    orderBusinessService.update(orderBusiness);
                }
            }
            if(orderBusiness.getOrderType() == 2){
                if(orderBusiness.getStatus() == 10){
                    orderBusiness.setStatus(15);
                    orderBusinessService.update(orderBusiness);
                }
            }

            //增加商品库存






            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCare/OrderBusinessToCancel!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    //取消已付款的订单(未发货或未服务的订单)
    @RequestMapping(value = "OrderBusinessToCancelAndRefund",method = RequestMethod.POST)
    @Transactional
    public MyResult OrderBusinessToCancelAndRefund(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String orderBusinessCode = jsonObject.getString("orderBusinessCode");

//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/OrderBusinessToCancelAndRefund");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            OrderBusiness orderBusiness = orderBusinessService.getByOrderBusinessCode(orderBusinessCode);
            Order order = orderService.getByOrderCode(orderBusiness.getOrderCode());
            OrderBusinessTradenoRel orderBusinessTradenoRel =
                    orderBusinessTradenoRelService.getByOrderBusinessCode(orderBusinessCode);
            OrderTradeno orderTradeno = orderTradenoService.getByTradeno(orderBusinessTradenoRel.getTradeNo());

            Elderly elderly = elderlyService.getByUserCode(order.getUserCode());


            if(orderBusiness == null){
                return failed("订单不存在");
            }


            //获取退款交易号
            String refundNo = "";

            OrderRefund orderRefund = orderRefundService.getByOrderBusinessCode(orderBusinessCode);
            if(orderRefund != null){
                if(orderRefund.getStatus() == 1){
                    return failed("已退款");
                }else{
                    refundNo = orderRefund.getRefundNo();
                }
            }else{
                refundNo = UUID.randomUUID().toString();

                orderRefund = new OrderRefund();
                orderRefund.setOrderRefundCode(UUID.randomUUID().toString());
                orderRefund.setUserCode(order.getUserCode());
                orderRefund.setOrderBusinessCode(orderBusinessCode);
                orderRefund.setTradeNo(orderBusinessTradenoRel.getTradeNo());
                orderRefund.setRefundNo(refundNo);
                orderRefund.setStatus(0);
                orderRefund.setCreateTime(new Date());
                orderRefundService.insert(orderRefund);

            }




            String msg="";

            //退款
            if(orderTradeno.getPayType().equals("yue")){

                orderRefund.setStatus(1);
                orderRefundService.update(orderRefund);


                elderly.setBalance(elderly.getBalance()+orderBusiness.getPrice());
                elderlyService.update(elderly);


                if(orderBusiness.getOrderType() == 1){
                    if(orderBusiness.getStatus() == 1){
                        orderBusiness.setStatus(5);
                        orderBusinessService.update(orderBusiness);
                    }
                }

                if(orderBusiness.getOrderType() == 2){
                    if(orderBusiness.getStatus() == 11 || orderBusiness.getStatus() == 12){
                        orderBusiness.setStatus(15);
                        orderBusinessService.update(orderBusiness);
                    }
                }

            }


            if(orderTradeno.getPayType().equals("wx")){
                Map<String, String> map = WechatPayUtils.wechatRefund(orderTradeno.getTradeNo(), orderTradeno.getPrice(), orderBusiness.getPrice(), orderRefund.getRefundNo());
                if (map.get("refundType").equals("success")){

                    orderRefund.setStatus(1);
                    orderRefundService.update(orderRefund);

                    if(orderBusiness.getOrderType() == 1){
                        if(orderBusiness.getStatus() == 1){
                            orderBusiness.setStatus(5);
                            orderBusinessService.update(orderBusiness);
                        }
                    }

                    if(orderBusiness.getOrderType() == 2){
                        if(orderBusiness.getStatus() == 11 || orderBusiness.getStatus() == 12){
                            orderBusiness.setStatus(15);
                            orderBusinessService.update(orderBusiness);
                        }
                    }
                    msg=map.get("msg");
                }else {
                    msg=map.get("msg");
                }

            }

            if(orderTradeno.getPayType().equals("ali")){
                Map<String, String> map = AliPayUtils.aliRefund(orderTradeno.getTradeNo(), orderBusiness.getPrice(), orderRefund.getRefundNo());
                if (map.get("refundType").equals("success")){

                    orderRefund.setStatus(1);
                    orderRefundService.update(orderRefund);

                    if(orderBusiness.getOrderType() == 1){
                        if(orderBusiness.getStatus() == 1){
                            orderBusiness.setStatus(5);
                            orderBusinessService.update(orderBusiness);
                        }
                    }

                    if(orderBusiness.getOrderType() == 2){
                        if(orderBusiness.getStatus() == 11 || orderBusiness.getStatus() == 12){
                            orderBusiness.setStatus(15);
                            orderBusinessService.update(orderBusiness);
                        }
                    }

                    msg=map.get("msg");
                }else {
                    msg=map.get("msg");
                }
            }

            //增加商品库存







            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("msg",msg);
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCare/OrderBusinessToCancelAndRefund!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }








    //订单 (职工政府)
    @RequestMapping(value = "OrderBusinessPageForGov",method = RequestMethod.POST)
    public MyResult OrderBusinessPageForGov(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            String businessCode = jsonObject.getString("businessCode");

//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/OrderBusinessPageForGov");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            //setGovernmentCodeByStaffCode(staffCode);
            Staff staff = staffService.getByStaffCode(staffCode);
            String governmentCode = staff.getGovernmentCode();



            PageInfo<OrderBusinessDTO> pageInfoOrderBusinessDTO =
                    orderBusinessService.getPageInfoOrderBusinessDTOByGovernmentCodeBusinessCode(pageIndex, pageSize,
                            governmentCode,businessCode);
            List<OrderBusinessDTO> listOrderBusinessDTO = pageInfoOrderBusinessDTO.getList();
            for(OrderBusinessDTO orderBusinessDTO:listOrderBusinessDTO){
                List<OrderGoodsDTO> listOrderGoodsDTO =
                        orderGoodsService.getListByOrderBusinessCode(orderBusinessDTO.getOrderBusinessCode());
                orderBusinessDTO.setListOrderGoodsDTO(listOrderGoodsDTO);
                String orderCode =orderBusinessDTO.getOrderCode();

                Order order =orderService.getByOrderCode(orderCode);

                User user =userService.getByUserCode(order.getUserCode());
                orderBusinessDTO.setUser(user);






                OrderServe orderServe =orderServeService.getByOrderBusinessCode(orderBusinessDTO.getOrderBusinessCode());
                if(orderServe==null){
                    continue;
                }
                orderBusinessDTO.setOrderServe(orderServe);
                String userCode1 =orderServe.getUserCode();
                Serve serve =serveService.getByUserCode(userCode1);
                orderBusinessDTO.setServe(serve);
            }


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfoOrderBusinessDTO",pageInfoOrderBusinessDTO);
            return success(jsonObject1);

        }catch (Exception e){
            insertError("/AppCare/OrderBusinessPageForGov",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }




    //写推送文件
    private void writePushFile(Business business,int orderType){

        try{

            //write txt file for business
            String sDirBusiness = pushBusiness;
            UserAppInfo userAppInfo = userAppInfoService.getByUserCode(business.getUserCode());
            if(userAppInfo != null){
                String os = userAppInfo.getAppOs();
                String regId = userAppInfo.getRegId();
                if(!os.equals("") && !regId.equals("")){
                    String part = "{\"os\":\"" + os + "\",\"channelId\":\"order\",\"title\":\"新订单\"," +
                            "\"content\":\"您有新的订单请查看\",\"regId\":\"" + regId + "\",\"type\":\"order\"}";

                    String fdPath = sDirBusiness+"\\wait";
                    File fd =new File(fdPath);
                    if(!fd.exists()){
                        fd.mkdirs();
                    }

                    File file = new File(fdPath+"\\"+getTimeStrSSSRandom()+".txt");
                    if(!file.exists()){
                        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8"); //gbk
                        BufferedWriter bw = new BufferedWriter(osw);
                        bw.write(part);
                        bw.close();
                        osw.close();
                    }

                }
            }


            //write txt file for service
            String sDirService = pushServe;
            if(orderType == 2){
                List<Serve> listServe = serveService.getListByBusinessCode(business.getBusinessCode());
                for(Serve serve : listServe){
                    UserAppInfo userAppInfoServe = userAppInfoService.getByUserCode(serve.getUserCode());
                    if(userAppInfoServe != null){
                        String os = userAppInfoServe.getAppOs();
                        String regId = userAppInfoServe.getRegId();
                        if(!os.equals("") && !regId.equals("")){
                            String part = "{\"os\":\"" + os + "\",\"channelId\":\"order\",\"title\":\"新订单\"," +
                                    "\"content\":\"订单来了\",\"regId\":\"" + regId + "\",\"type\":\"order\"}";

                            String fdPath = sDirService+"\\wait";
                            File fd =new File(fdPath);
                            if(!fd.exists()){
                                fd.mkdirs();
                            }

                            File file = new File(fdPath+"\\"+getTimeStrSSSRandom()+".txt");
                            if(!file.exists()){
                                OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
                                BufferedWriter bw = new BufferedWriter(osw);
                                bw.write(part);
                                bw.close();
                                osw.close();
                            }

                        }
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }




    @RequestMapping(value = "Test", method = RequestMethod.GET)
    public MyResult Test() {

        try{
            String timestr = "12123123";
            String sign = getSign2(timestr);

            HashMap<String,Object> hm = new HashMap<>();
            hm.put("timestr",timestr);
            hm.put("sign",sign);
            hm.put("phone","1111");
            hm.put("imei","1111");
            hm.put("platform","shop");

            String rv = postTemplate("http://117.71.50.36:7001/api/ToGWatch/Add",hm);


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("rv",rv);
            return success(jsonObject1);
        }catch (Exception e){
            return anomalous(getExceptionMsg(e));
        }

    }
    //呼叫中心下单
    @RequestMapping(value = "CallSubmit",method = RequestMethod.POST)
    @Transactional
    public MyResult CallSubmit(@RequestBody JSONObject jsonObject,HttpServletRequest request) {

        try{

            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String elderlyCode = jsonObject.getString("elderlyCode");
            Double totalPrice = jsonObject.getDouble("totalPrice");
            JSONArray goodsList = jsonObject.getJSONArray("goodsList");
            String userAddressCode = jsonObject.getString("userAddressCode");
            String payType = "yue";

//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCare/CallSubmit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            if(goodsList.size()==0){
                return failed("请选择商品！");
            }

            //setGovernmentCodeByStaffCode(staffCode);
            Staff staff = staffService.getByStaffCode(staffCode);
            String governmentCode = staff.getGovernmentCode();

            ElderlyDTO elderly = elderlyService.getByElderlyCode(elderlyCode);
            User user = userService.getByUserCode(elderly.getUserCode());
            UserAddress userAddress = userAddressService.getByUserAddressCode(userAddressCode);
            if(userAddress == null){
                return failed("收货地址不存在");
            }

            //支付交易号
            String tradeno = "sn"+ getTimeStrSSS()+ new Random().nextInt(100000);
            //订单流水号
            String serialNumber = getTimeStrSSS() +user.getPhone()+ new Random().nextInt(100000);


//            Cart cart = cartService.getByUserCodeGovernmentCode(user.getUserCode(),governmentCode);
//            if(cart == null){
//                return failed("购物车为空");
//            }
//            cartService.delete(cart.getCartCode());


            //订单的支付交易号
            OrderTradeno orderTradeno = new OrderTradeno();
            orderTradeno.setOrderTradenoCode(UUID.randomUUID().toString());
            orderTradeno.setUserCode(user.getUserCode());
            orderTradeno.setTradeNo(tradeno);
            orderTradeno.setPrice(totalPrice);
            orderTradeno.setCreateTime(new Date());
            orderTradeno.setStatus(0);
            orderTradeno.setPayType(payType);
            orderTradeno.setClientType("call");
            orderTradenoService.insert(orderTradeno);


            //订单
            Order order = new Order();
            order.setOrderCode(UUID.randomUUID().toString());
            order.setUserCode(user.getUserCode());
            order.setPrice(totalPrice);
            order.setCreateTime(new Date());
            order.setReceiveName(userAddress.getReceiveName());
            order.setReceivePhone(userAddress.getReceivePhone());
            order.setReceiveArea(userAddress.getAreaName());
            order.setReceiveAreaPath(userAddress.getAreaNamePath());
            order.setReceiveAddress(userAddress.getReceiveAddress());
            order.setGovernmentCode(governmentCode);
            order.setCashierElderlyCode(emptyUUID);
            orderService.insert(order);






//            List<CartBusiness> listCartBusiness = cartBusinessService.getListByCartCode(cart.getCartCode());

//            String goodsNames="";
//            String goodsNameMores="";

            for(int i = 0; i < goodsList.size(); i++){
//                cartBusinessService.delete(cartBusiness.getCartBusinessCode());
                JSONObject goodsBusiness = goodsList.getJSONObject(i);

                Business business = businessService.getByBusinessCode(goodsBusiness.getString("businessCode"));
                User userBusiness = userService.getByUserCode(business.getUserCode());


                //订单的商家
                OrderBusiness orderBusiness = new OrderBusiness();
                orderBusiness.setOrderBusinessCode(UUID.randomUUID().toString());
                orderBusiness.setOrderCode(order.getOrderCode());
                orderBusiness.setBusinessCode(goodsBusiness.getString("businessCode"));
                orderBusiness.setBusinessName(userBusiness.getName());
                orderBusiness.setPrice(goodsBusiness.getDouble("price"));
                orderBusiness.setCreateTime(new Date());
                int goodsType=goodsBusiness.getInteger("goodsType");
                orderBusiness.setOrderType(goodsType);
                if(goodsType == 1){
                    orderBusiness.setStatus(0);
                }
                if(goodsType == 2){
                    orderBusiness.setStatus(10);
                }
                orderBusiness.setSerialNumber(serialNumber);
                orderBusiness.setPayType(payType);

                orderBusiness.setOrderFrom("call");

                orderBusinessService.insert(orderBusiness);


                //商家的订单与支付交易号关联
                OrderBusinessTradenoRel orderBusinessTradenoRel = new OrderBusinessTradenoRel();
                orderBusinessTradenoRel.setOrderBusinessTradeNoRelCode(UUID.randomUUID().toString());
                orderBusinessTradenoRel.setTradeNo(tradeno);
                orderBusinessTradenoRel.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                orderBusinessTradenoRelService.insert(orderBusinessTradenoRel);


                int orderType = 0;
                double priceOrder = orderBusiness.getPrice();
                JSONArray goodsInfoList = goodsBusiness.getJSONArray("goodsInfoList");
//                List<CartGoods> listCartGoods = cartGoodsService.getListByCartBusinessCode(cartBusiness.getCartBusinessCode());
                for(int j = 0; j < goodsInfoList.size(); j++){
//                    cartGoodsService.delete(cartGoods.getCartGoodsCode());
                    JSONObject goodsInfo = goodsInfoList.getJSONObject(j);
                    String goodsCode = goodsInfo.getString("goodsCode");
                    int number = goodsInfo.getInteger("number");
//                    Date planTime = goodsInfo.getDate("planTime");

//                    Double price = goodsInfo.getDouble("price");
                    String goodsPriceCode = goodsInfo.getString("goodsPriceCode");
//                    String goodsSizeCode = goodsInfo.getString("goodsSizeCode");
                    GoodsPrice goodsPrice = goodsPriceService.getByGoodsPriceCode(goodsPriceCode);
                    Goods goods = goodsService.getByGoodsCode(goodsCode);
                    Date planTime = null;
                    if(goods.getGoodsType()==2){
                        orderType = goods.getGoodsType();
                        planTime = goodsInfo.getDate("planTime");
//                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//                        planTime = sdf.parse(planTimeStr);
                    }
//                    goodsNames+=goods.getName()+",";
//                    goodsNameMores+=goods.getNameMore()+",";

//                    CartGoodsPrice cartGoodsPrice =
//                            cartGoodsPriceService.getByCartGoodsCode(cartGoods.getCartGoodsCode());
//                    int goodsType = jsonObject.getInteger("goodsType");

                    //订单的商品
                    OrderGoods orderGoods = new OrderGoods();
                    orderGoods.setOrderGoodsCode(UUID.randomUUID().toString());
                    orderGoods.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                    orderGoods.setGoodsCode(goodsCode);
                    orderGoods.setNumber(number);
                    orderGoods.setPlanTime(planTime);
                    orderGoods.setGoodsName(goods.getName());
                    orderGoods.setGoodsPrice(goodsPrice.getPrice());
                    orderGoods.setGoodsImg(goods.getHead1());
                    orderGoodsService.insert(orderGoods);



                    //订单商品的属性
                    OrderGoodsPrice orderGoodsPrice = new OrderGoodsPrice();
                    orderGoodsPrice.setOrderGoodsPriceCode(UUID.randomUUID().toString());
                    orderGoodsPrice.setOrderGoodsCode(orderGoods.getOrderGoodsCode());
                    orderGoodsPrice.setPrice(goodsPrice.getPrice());
//                    goodsSizeSgetByGoodsPriceCode

                    String goodsSizeName = "";
                    if(!goodsPrice.getGoodsSizeCode().equals("")){
                        goodsSizeName = goodsSizeService.getByGoodsSizeCode(goodsPrice.getGoodsSizeCode()).getName();
                    }
                    orderGoodsPrice.setGoodsSizeCode(goodsPrice.getGoodsSizeCode());
                    orderGoodsPrice.setGoodsSizeName(goodsSizeName);

                    String goodsColorName = "";
                    if(!goodsPrice.getGoodsColorCode().equals("")){
                        goodsColorName = goodsColorService.getByGoodsColorCode(goodsPrice.getGoodsColorCode()).getName();
                    }
                    orderGoodsPrice.setGoodsColorCode(goodsPrice.getGoodsColorCode());
                    orderGoodsPrice.setGoodsColorName(goodsColorName);
                    orderGoodsPriceService.insert(orderGoodsPrice);




                    //增加库存
                    if(goods.getStock() > goods.getSaleCount()+number){
                        goods.setSaleCount(goods.getSaleCount()+number);
                        goodsService.update(goods);
                    }

                }

                writePushFile(business,orderType);

                elderly.setBalance(elderly.getBalance() - priceOrder);
                elderlyService.update(elderly);


            }

//            goodsNames=goodsNames.substring(0,goodsNames.length()-1);
//            goodsNameMores=goodsNameMores.substring(0,goodsNameMores.length()-1);
//
//            if (goodsNames.length()>256){
//                goodsNames=goodsNames.substring(0,256);
//            }
//            if (goodsNameMores.length()>1000){
//                goodsNames=goodsNameMores.substring(0,1000);
//            }



            String appUseStr = "";
            Map wxPayMap=null;


            //修改总订单的支付状态
            OrderTradeno orderTradeno1 = orderTradenoService.getByTradeno(tradeno);
            if(orderTradeno1.getStatus() == 0){
                orderTradeno1.setStatus(1);
                orderTradenoService.update(orderTradeno1);
            }


            //交易号关联的订单
            List<OrderBusinessTradenoRel> listOrderBusinessTradeoRel =
                    orderBusinessTradenoRelService.getListByTradeno(tradeno);

            //修改订单的状态
            for(OrderBusinessTradenoRel orderBusinessTradenoRel2:listOrderBusinessTradeoRel){
                OrderBusiness orderBusiness2 =
                        orderBusinessService.getByOrderBusinessCode(orderBusinessTradenoRel2.getOrderBusinessCode());
                if (orderBusiness2.getOrderType()==1){
                    if(orderBusiness2.getStatus() == 0){
                        orderBusiness2.setStatus(1);
                        orderBusiness2.setPayTime(new Date());
                        orderBusinessService.update(orderBusiness2);
                    }
                }
                if (orderBusiness2.getOrderType()==2){
                    if(orderBusiness2.getStatus() == 10){
                        orderBusiness2.setStatus(11);
                        orderBusiness2.setPayTime(new Date());
                        orderBusinessService.update(orderBusiness2);
                    }
                }
            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("payType",payType);
            jsonObject1.put("appUseStr",appUseStr);
            jsonObject1.put("wxPayMap",wxPayMap);
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCare/CartSubmit!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }







}
